package com.ivncorp.evnproject.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import de.greenrobot.dao.AbstractDaoMaster;
import de.greenrobot.dao.identityscope.IdentityScopeType;

public class DaoMaster extends AbstractDaoMaster{
	
	public static final int SCHEMA_VERSION = 1000;
	
	public static void createAllTables(SQLiteDatabase db, boolean ifNotExists){
		TuTTDao.createTable(db, ifNotExists);
		CotDao.createTable(db, ifNotExists);
		TramDao.createTable(db, ifNotExists);
	}
	
	public static void dropAllTable(SQLiteDatabase db, boolean ifExists){
		TuTTDao.dropTable(db, ifExists);
		CotDao.dropTable(db, ifExists);
		TramDao.dropTable(db, ifExists);
	}
	
	public static abstract class OpenHelper extends SQLiteOpenHelper{

		public OpenHelper(Context context, String name, CursorFactory factory) {
			super(context, name, factory, SCHEMA_VERSION);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db) {
			createAllTables(db, false);
		}
	}
	
	public static class DevOpenHelper extends OpenHelper{

		public DevOpenHelper(Context context, String name,
				CursorFactory factory) {
			super(context, name, factory);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			dropAllTable(db, true);
			onCreate(db);
		}
		
	}

	public DaoMaster(SQLiteDatabase db) {
		super(db, SCHEMA_VERSION);
		registerDaoClass(TuTTDao.class);
		registerDaoClass(CotDao.class);
		registerDaoClass(TramDao.class);
	}
	
	public DaoSession newSession(){
		return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
	}
	
	public DaoSession newSession(IdentityScopeType type) {
	    return new DaoSession(db, type, daoConfigMap);
	}

}
